package cn.gl.basic.controller;

import cn.gl.basic.dto.EmailCodeDto;
import cn.gl.basic.dto.PhoneCodeDto;
import cn.gl.basic.exception.BusinessException;
import cn.gl.basic.service.IVerifyCodeService;
import cn.gl.basic.util.AjaxResult;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verify_code")
public  class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            String basse64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setObj(basse64Str);
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto phoneCodeDto){
        try {
            verifyCodeService.getPhoneCode(phoneCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }
    @PostMapping("/email")
    public AjaxResult getemailCode(@RequestBody EmailCodeDto emailCodeDto){
        try {
            verifyCodeService.getemailCode(emailCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }

    /**
     * 微信绑定的时候发送手机验证码
     * @param phoneCodeDto
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody PhoneCodeDto phoneCodeDto) {
        try {
            verifyCodeService.getBinderSmsCode(phoneCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }

}
